// 云函数/commemorativeCard/index.js
'use strict';
const db = uniCloud.database()

exports.main = async (event, context) => {
  const collection = db.collection('commemorativeCard')
  const { action, data, id } = event
console.log(data)
  try {
    switch (action) {
      case 'list':
        const res = await collection.orderBy('requiredPoints', 'asc').get()
        return { 
          code: 200, 
          data: {
            list: res.data,
            total: res.data.length
          }
        }
        
      case 'create':
        const createRes = await collection.add({
          ...data,
          createTime: Date.now(),
          isReceived: false,
          canGet: false,
        })
        return { code: 200, data: createRes }
        
      case 'update':
        const updateRes = await collection.doc(data._id).update({
          name:data.name,
          requiredPoints:data.requiredPoints,
          image:data.image,
          desc:data.desc,
          updateTime: Date.now()
        })
        return { code: 200, data: updateRes }
        
      case 'delete':
        const deleteRes = await collection.doc(id).remove()
        return { code: 200, data: deleteRes }
        
      default:
        return { code: 400, message: '无效操作' }
    }
  } catch (error) {
    return { code: 500, message: error.message }
  }
}